//
//  LZSettingsViewController.m
//  laziz_Consumer
//
//  Created by XMac on 16/10/28.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZSettingsViewController.h"
#import "LZSettingsTableViewCell.h"
#import "LZNotificationSettingViewController.h"
#import "LZReportProblemViewController.h"
#import "UIViewController+InitWithdDefaultNib.h"
#import "SDWebImageManager.h"
#import "MBProgressHUD+LZCustomMBProgressHUD.h"
#import "LZActionSheet.h"
#import "Masonry.h"
#import "LZCommonWebViewController.h"
#import "LZRemindView.h"

@interface LZSettingsViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic , strong) NSArray *titles;

@property (strong, nonatomic) IBOutlet UITableView *settingTableView;

@property (nonatomic, copy) NSString * cacheString;

@property (weak, nonatomic) LZActionSheet * sheetView;

@end

@implementation LZSettingsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.umLogPageViewName = @"设置页面";
    
    [self configData];
    
    [self configUI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


#pragma
#pragma mark private method

-(void)configData{
    _titles = @[@[LZLocalizedString(@"setting_notification", @""),LZLocalizedString(@"setting_clear_cache", nil)],@[LZLocalizedString(@"setting_report_problem", nil),LZLocalizedString(@"setting_about_us", nil)]];
    
    // ****************** 计算缓存大小 ******************
    NSUInteger cacheSize = [[SDImageCache sharedImageCache] getSize];
    self.cacheString = [self fileSizeWithInterge:cacheSize];
}

-(void)configUI{
    [self registerCell];
    
    self.title = LZLocalizedString(@"mine_setting", nil);
    
    _settingTableView.tableFooterView = [UIView new];
}

-(void)registerCell{
    
    [_settingTableView registerNib:[UINib nibWithNibName:@"LZSettingsTableViewCell" bundle:nil] forCellReuseIdentifier:@"LZSettingsTableViewCell"];
    
}


#pragma
#pragma mark UITableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.titles.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *subTitles = self.titles[section];
    return subTitles.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 55;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LZSettingsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LZSettingsTableViewCell"];
    
    
    if (indexPath.section == 0 && indexPath.row == 1) {
        [cell showDetailLabelWithText:self.cacheString];
    }
    else {
        [cell showDetailLabelWithText:nil];
    }
    
    [cell showLine:YES];
    
    if (indexPath.section == 0 && indexPath.row == 1) {
        [cell showLine:NO];
    }
    else {
        if (indexPath.row == 1) {
            [cell showLine:NO];
        }
    }
    
    [cell changeTitle:_titles[indexPath.section][indexPath.row]];
    
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            LZNotificationSettingViewController *vc = [[LZNotificationSettingViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        }
        else if (indexPath.row == 1) {
            // 清除缓存
            [self.sheetView creatHeaderWithTitle:LZLocalizedString(@"setting_clear_remind", nil) andHeight:50];
            [self.sheetView ReloadTableViewArrayTitle:@[@[LZLocalizedString(@"setting_button_clear", nil)],@[LZLocalizedString(@"cancel", nil)]]];
            [self.sheetView ShowView];
            
        }
    }
    else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            // 意见反馈
            LZReportProblemViewController *reportVC = [[LZReportProblemViewController alloc]initWithDefaultNib];
            [self.navigationController pushViewController:reportVC animated:YES];
        }
        else if (indexPath.row == 1) {
            // 关于我们
            LZCommonWebViewController *aboutUsVc = [[LZCommonWebViewController alloc]initWithDefaultNib];
            aboutUsVc.webCode = kWebState_aboutUs;
            [self.navigationController pushViewController:aboutUsVc animated:YES];
        }
    }
}

- (LZActionSheet*)sheetView {
    if (_sheetView) {
        return _sheetView;
    }
    
    LZActionSheet *sheet = [[LZActionSheet alloc] initWithFrame:self.navigationController.view.bounds];
    _sheetView = sheet;
    [self.navigationController.view addSubview:_sheetView];
    
    WS(weakSelf)
    _sheetView.ClickSheetBlock = ^(NSIndexPath *indexPath, id obj) {
        if (indexPath.section == 0) {
            [weakSelf clearAction];
        }
    };
    
    return _sheetView;
}

- (void)clearAction {
    MBProgressHUD *hub = [MBProgressHUD showAndSetup:[UIImage imageNamed:@"dustbin_icon"] text:LZLocalizedString(@"setting_clearing", nil) context:self.view];
    
    [[SDWebImageManager sharedManager].imageCache clearDiskOnCompletion:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            // ****************** 计算缓存大小 ******************
            NSUInteger cacheSize = [[SDImageCache sharedImageCache] getSize];
            self.cacheString = [self fileSizeWithInterge:cacheSize];
            [self.settingTableView reloadData];
            [hub hideAnimated:NO];
            
            [LZRemindView initWithImage:[UIImage imageNamed:@"提示对勾"] andContent:LZLocalizedString(@"setting_clear_success", @"")];

        });
    }];
    
    [self cleanCacheAndCookie];
}

/**清除缓存和cookie*/
- (void)cleanCacheAndCookie{
    //清除cookies
    NSHTTPCookie *cookie;
    NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    for (cookie in [storage cookies]){
        [storage deleteCookie:cookie];
    }
    //清除UIWebView的缓存
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    NSURLCache * cache = [NSURLCache sharedURLCache];
    [cache removeAllCachedResponses];
    [cache setDiskCapacity:0];
    [cache setMemoryCapacity:0];
}

//计算出大小
- (NSString *)fileSizeWithInterge:(NSInteger)size{
    // 1k = 1024, 1m = 1024k
    if (size < 1024) {// 小于1k
        return [NSString stringWithFormat:@"%ldB",(long)size];
    }else if (size < 1024 * 1024){// 小于1m
        CGFloat aFloat = size/1024;
        return [NSString stringWithFormat:@"%.0fK",aFloat];
    }else if (size < 1024 * 1024 * 1024){// 小于1G
        CGFloat aFloat = size/(1024 * 1024);
        return [NSString stringWithFormat:@"%.1fM",aFloat];
    }else{
        CGFloat aFloat = size/(1024*1024*1024);
        return [NSString stringWithFormat:@"%.1fG",aFloat];
    }
}

@end
